package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"softwareEngineering/models"
)

type RegisterController struct {
	beego.Controller
}

func (c *RegisterController) Register() {
	c.TplName = "register.html"
}

func (c *RegisterController) HandleRegister() {
	username := c.GetString("username")
	userpw := c.GetString("userpw")
	userrole := c.GetString("userrole")
	userlevel := " "
	switch userrole {
	case "超级管理员":
		userlevel = "3"
	case "管理员":
		userlevel = "2"
	case "普通用户":
		userlevel = "1"
	}
	o := orm.NewOrm()
	user := models.User{}
	user.Name = username
	user.Pwd = userpw
	user.Role = userrole
	user.Level = userlevel
	//检查用户是否已经在数据库中
	er := o.Read(&user, "Name")
	if er != nil {
		_, err := o.Insert(&user)
		if err != nil {
			beego.Info("插入失败")
			c.Data["ur"] = ""
			c.TplName = "register.html"
		} else {
			beego.Info("插入成功")
			c.Data["ur"] = "user register success"
			c.TplName = "imageShow.html"
		}
	} else {
		beego.Info("用户已经存在")
		c.Ctx.WriteString("用户已经存在！！")

	}

}
